package controllers

import (
	"encoding/json"
	"fmt"
	"gin-system/models"
	"gin-system/pkg/app"
	"gin-system/pkg/e"
	"gin-system/services"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"time"
)

func Get(c *gin.Context)  {
	physical := models.Physical{}
	if arg := c.Query("student_id"); arg != "" {
		physical.StudentId = com.StrTo(arg).MustInt()
	}
	if arg := c.Query("id"); arg != "" {
		physical.Id = com.StrTo(arg).MustInt()
	}
	if arg := c.Query("student_name"); arg != "" {
		physical.StudentName = arg
	}
	if arg := c.Query("faculty"); arg != "" {
		physical.Faculty = arg
	}
	if arg := c.Query("age"); arg != "" {
		physical.Age = com.StrTo(arg).MustInt()
	}
	physicalList := services.GetAll(physical)
	app.OK(c, physicalList,"查询成功")
}

func Del(c *gin.Context){
	id := -1
	if arg := c.Query("id"); arg != "" {
		id = com.StrTo(arg).MustInt()
	}
	if id == -1{
		app.INFO(c,30001,"参数错误")
		return
	}
	err := services.Del(id)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"删除成功")
}

func Update(c *gin.Context){

	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	physical := models.Physical{}
	if c.Query("id") != "" {
		physical.Id = com.StrTo(c.Query("id")).MustInt()
	}else{
		app.INFO(c, 30000, "参数非法")
		return
	}
	if c.Query("student_id") != "" {
		physical.StudentId = com.StrTo(c.Query("student_id")).MustInt()
	}
	if c.Query("student_name") != "" {
		physical.StudentName = c.Query("student_name")
	}
	if c.Query("sex") != ""{
		physical.Sex = com.StrTo(c.Query("sex")).MustInt()
	}
	if c.Query("age") != ""{
		physical.Age = com.StrTo(c.Query("age")).MustInt()
	}
	if c.Query("height") != "" {
		physical.Height = com.StrTo(c.Query("height")).MustInt()
	}
	if c.Query("chest") != ""  {
		physical.Chest = com.StrTo(c.Query("chest")).MustInt()
	}
	if c.Query("faculty") != "" {
		physical.Faculty = c.Query("faculty")
	}
	if c.Query("diagnosis") != "" {
		physical.Diagnosis = c.Query("diagnosis")
	}
	physical.UpdateTime = time.Now()
	err := services.Update(physical)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"更新成功")
}

func Add(c *gin.Context){
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)

	physical := models.Physical{}
	if c.Query("student_id") != "" {
		physical.StudentId = com.StrTo(c.Query("student_id")).MustInt()
	}
	if c.Query("student_name") != "" {
		physical.StudentName = c.Query("student_name")
	}
	if c.Query("sex") != ""{
		physical.Sex = com.StrTo(c.Query("sex")).MustInt()
	}
	if c.Query("age") != ""{
		physical.Age = com.StrTo(c.Query("age")).MustInt()
	}
	if c.Query("height") != "" {
		physical.Height = com.StrTo(c.Query("height")).MustInt()
	}
	if c.Query("chest") != ""  {
		physical.Chest = com.StrTo(c.Query("chest")).MustInt()
	}
	if c.Query("faculty") != "" {
		physical.Faculty = c.Query("faculty")
	}
	if c.Query("diagnosis") != "" {
		physical.Diagnosis = c.Query("diagnosis")
	}
	physical.UpdateTime = time.Now()
	physical.CreateTime = time.Now().Year()
	err := services.Add(physical)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"添加成功")
}

func GetAverage(c *gin.Context)  {
	err, data := services.GetAverage()
	if err != nil {
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c,data,"获取成功")
}


func FileDownload(c *gin.Context){
	physical := models.Physical{}
	if arg := c.Query("studentId"); arg != "" {
		physical.StudentId = com.StrTo(arg).MustInt()
	}
	if arg := c.Query("id"); arg != "" {
		physical.Id = com.StrTo(arg).MustInt()
	}
	if arg := c.Query("student_mame"); arg != "" {
		physical.StudentName = arg
	}
	if arg := c.Query("faculty"); arg != "" {
		physical.Faculty = arg
	}
	if arg := c.Query("age"); arg != "" {
		physical.Age = com.StrTo(arg).MustInt()
	}
	services.ExportFIle(physical)
	c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "work.xlsx"))
	c.Writer.Header().Add("Content-Type", "application/octet-stream")
	c.File("./work.xlsx")
}
func GetConsecutiveMean(c *gin.Context)  {
	err, data := services.GetConsecutiveMean()
	if err != nil {
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c,data,"获取成功")
}
